package com.petdog.module.auth.controller;

import com.petdog.common.dto.ApiResponse;
import com.petdog.module.auth.dto.CodeLoginRequest;
import com.petdog.module.auth.dto.LoginRequest;
import com.petdog.module.auth.dto.LoginResponse;
import com.petdog.module.auth.dto.RegisterRequest;
import com.petdog.module.auth.dto.SendVerificationCodeRequest;
import com.petdog.module.auth.dto.TokenRefreshRequest;
import com.petdog.module.auth.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 认证控制器
 * 处理用户登录、注册、刷新令牌等认证相关请求
 */
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 用户登录接口
     * @param loginRequest 登录请求参数
     * @return 登录响应
     */
    @PostMapping("/login")
    public ResponseEntity<LoginResponse> login(@Validated @RequestBody LoginRequest loginRequest) {
        LoginResponse response = authService.login(loginRequest);
        return ResponseEntity.ok(response);
    }

    /**
     * 用户注册接口
     * @param registerRequest 注册请求参数
     * @return 注册响应
     */
    @PostMapping("/register")
    public ResponseEntity<LoginResponse> register(@Validated @RequestBody RegisterRequest registerRequest) {
        LoginResponse response = authService.register(registerRequest);
        return ResponseEntity.ok(response);
    }

    /**
     * 刷新令牌接口
     * @param tokenRefreshRequest 刷新令牌请求参数
     * @return 新的令牌响应
     */
    @PostMapping("/refresh")
    public ResponseEntity<LoginResponse> refreshToken(@Validated @RequestBody TokenRefreshRequest tokenRefreshRequest) {
        LoginResponse response = authService.refreshToken(tokenRefreshRequest);
        return ResponseEntity.ok(response);
    }
    
    /**
     * 发送邮箱验证码接口
     * @param request 发送验证码请求参数
     * @return 操作结果
     */
    @PostMapping("/send-code")
    public ResponseEntity<ApiResponse<String>> sendVerificationCode(@Validated @RequestBody SendVerificationCodeRequest request) {
        ApiResponse<String> response = authService.sendVerificationCode(request);
        return ResponseEntity.ok(response);
    }
    
    /**
     * 发送登录验证码接口
     * @param email 接收验证码的邮箱
     * @return 操作结果
     */
    @PostMapping("/code/login")
    public ResponseEntity<ApiResponse<String>> sendLoginVerificationCode(@RequestParam("email") String email) {
        ApiResponse<String> response = authService.sendLoginVerificationCode(email);
        return ResponseEntity.ok(response);
    }
    
    /**
     * 验证码登录接口
     * @param codeLoginRequest 验证码登录请求参数
     * @return 登录响应
     */
    @PostMapping("/login/code")
    public ResponseEntity<LoginResponse> codeLogin(@Validated @RequestBody CodeLoginRequest codeLoginRequest) {
        LoginResponse response = authService.codeLogin(codeLoginRequest);
        return ResponseEntity.ok(response);
    }
}
